
import { baseRequest as request } from './http'
import { getLoginInfo } from '../uses'
import { OptionType } from '../../types/services/http'
import env from "../constants/env.json";
import md5 from "js-md5";


type requestParamsProps = {
  url: string
  data: any
  contentType?: string
}

const baseOptions = (params: requestParamsProps, method = "GET", config = { }) => {
  let { url, data } = params;
  let contentType = "application/json";
  contentType = params.contentType || contentType;
  //请求头加devKey
  let timeStamp = new Date().getTime().toString();
  let md5TimeStamp = md5(`${timeStamp}lajsf.com`);
  console.log(' - - - - - - - - config : ', config);
  const option: OptionType = {
    url: config?.url ? config?.url + url : url.indexOf("http") !== -1 ? url : env.httpBaseUrl + url,
    data: data,
    method: method,
    header: {
      "content-type": contentType,
      tenantId: "nutrition-plan",
      devType: "4",
      userId: "922284423446528",
      token: "mNNaEJB0awCA1625033238328",
      devKey: `${timeStamp}${md5TimeStamp}`,
      appId: "diet",
      cooperationId: "1",
      ...config?.headers
    },
    xhrFields: { withCredentials: true }
  };

  return option;
};

export const httpGet = (url: string, data?: object): Promise<any> => {
  let option = baseOptions({ url, data });
  return request(option);
};

export const httpPost = (url: string, data?: object, config?: object): Promise<any> => {
  let option = baseOptions({ url, data }, "POST", config);
  return request(option);
};

export const httpDelete = (url: string, data?: object): Promise<any> => {
  let option = baseOptions({ url, data }, "DELETE");
  return request(option);
};

export const httpPut = (url: string, data?: object): Promise<any> => {
  let option = baseOptions({ url, data }, "PUT");
  return request(option);
};
